      SUBROUTINE TSHADOW1(T50,POS,VEL,KCOORD,
     *    TPRISE,TPSET,TNRISE,TNSET,NOWSTAT, LUERR,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C GIVEN A S/C STATE AND TIME, THIS ROUTINE COMPUTES THE APPROXIMATE
C TIMES OF THE PREVIOUS AND NEXT SUNRISE AND SUNSET. THIS ROUTINE USES
C ANALYTICAL EXPRESSIONS FOR THE COMPUTATIONS. THE SUN POSITION IS
C FIXED AT TIME T50 AND THE ORBIT PROPAGATION IS KEPLERIAN. 'PREVIOUS'
C AND 'NEXT' ARE WITHIN ONE ORBITAL PERIOD OF THE INPUT TIME, T50.
C
C
C VARIABLE DIM TYPE I/O DESCRIPTION
C -------- --- ---- --- -----------
C
C T50       1   R*8  I  THE TIME FOR WHICH THE S/C STATE IS SUPPLIED.
C                       IN SECONDS SINCE 1/1/50, 0.0 HRS UT.
C
C POS       3   R*8  I  THE S/C POSITION VECTOR. IN KM.
C
C VEL       3   R*8  I  THE S/C VELOCITY VECTOR. IN KM/SEC.
C
C KCOORD    1   I*4  I  FLAG INDICATING THE COORDINATE SYSTEM IN WHICH
C                       POS AND VEL ARE GIVEN. ALWAYS GEOCENTRIC.
C
C                        = 1, MEAN EQUATOR AND EQUINOX OF 1950.0
C                        = 2, MEAN EQUATOR AND EQUINOX OF DATE
C                        = 3, TRUE EQUATOR AND EQUINOX OF DATE
C                        = OTHERWISE, ERROR. IERR=1 IS SET.
C
C TPRISE    1   R*8  O  TIME OF THE PREVIOUS SUNRISE. IN SECONDS SINCE
C                       1/1/50, 0.0 HRS. TPRISE < T50.
C
C TPSET     1   R*8  O  TIME OF THE PREVIOUS SUNSET. IN SECONDS SINCE
C                       1/1/50, 0.0 HRS. TPSET < T50.
C
C TNRISE    1   R*8  O  TIME OF THE NEXT SUNRISE. IN SECONDS SINCE
C                       1/1/50, 0.0 HRS. TNRISE > T50.
C
C TNSET     1   R*8  O  TIME OF THE NEXT SUNSET. IN SECONDS SINCE
C                       1/1/50, 0.0 HRS. TNSET > T50.
C
C NOWSTAT   1   I*4  O  CURRENT DAY/NIGHT STATUS.
C                       = 1, IN FULL SUN OR PENUMBRA(=PART SHADOW)
C                       = 2, IN UMBRA(=FULL SHADOW)
C
C LUERR     1   I*4  I  FORTRAN UNIT NUMBER FOR ERROR MESSAGES.
C
C IERR      1   I*4  O  ERROR RETURN FLAG.
C
C                       = 0, NO ERROR.
C
C                       =-1, MINOR ERROR. S/C DOES NOT HAVE ORBIT NIGHT,
C                            AT LEAST NOT WITHIN ONE ORBITAL PERIOD OF
C                            T50. ALL RISE/SET TIMES SET TO T50.
C
C                       = 1, BIG ERROR. RESULTS NOT USEFUL. ALL OTHER
C                            OUTPUT PARAMETERS ARE SET TO ZERO.
C
C***********************************************************************
C
C BY C PETRUZZO. GSFC/742. 9/84.
C   MODIFIED......
C
C***********************************************************************
C
C
      REAL*8 POS(3),VEL(3)
      REAL*8 RSUN(3),ELEMS(6)
      REAL*8 TSUN/-1.D10/,DTMAX/900.D0/
      INTEGER KCOORDL/-99/,INIT/1/
      REAL*8 TWOPI/ 6.283185307179586D0 /
      REAL*8 DEGRAD/ 57.29577951308232D0 /
      LOGICAL GETSUN
C
C
      IBUG = 0
      LUBUG = 19
C
      IF(IBUG.NE.0) WRITE(LUBUG,1001) PAKTIM50(T50),POS,VEL
 1001 FORMAT(/,' TSHADOW1 DEBUG 1001. TIME=',F14.6/,
     *   '   POS=',3G15.7/,'   VEL=',3G15.7)
C
C ERROR CHECK.
C
      IF(KCOORD.LT.1 .OR. KCOORD.GT.3) THEN
        IERR = 1
        IF(LUERR.GT.0) WRITE(LUERR,5001) KCOORD
 5001   FORMAT(/,' TSHADOW1 ERROR. BAD KCOORD INPUT. VALUE=',I5/)
        GO TO 9999
        END IF
C
C INITIALIZE.
C
      IERR = 0
      ERTHMU = CONST(56)
C
C GET THE SUN POSITION.
C
      GETSUN = DABS(TSUN-T50).GT.DTMAX  .OR. KCOORD.NE.KCOORDL
      IF(GETSUN) THEN
        TSUN = T50
        KCOORDL = KCOORD
        CALL SOLGEN(KCOORD,TSUN,RSUN,0)
        END IF
C
      IF(IBUG.NE.0) WRITE(LUBUG,1002) PAKTIM50(TSUN),RSUN
 1002 FORMAT(/,' TSHADOW1 DEBUG 1002. SUN POS TIME=',F14.6/,
     *    '   SUN POS=',3G15.7)
C
C CONVERT POS/VEL TO KEPLERIAN ELEMENTS FOR USE IN SHADTA. GET PERIOD.
C
      CALL TOKEPL(ERTHMU,POS,VEL,ELEMS,DUM,DUM)
      PERIOD = TWOPI * ELEMS(1) * DSQRT(ELEMS(1)/ERTHMU)
C
C GET TRUE ANOMALIES OF SUNRISE AND SUNSET.
C
      KUMB = 1
      CALL SHADTA(ELEMS,RSUN,KUMB,-1,UMBINTA,UMBOUTTA,DUM,DUM,
     *       LUERR,IER1)
      IF(IER1.NE.0 .OR. KUMB.EQ.0) THEN
        IERR = 1
        GO TO 9999
        END IF
      IF(KUMB.EQ.1) THEN
C      NO ORBIT NIGHTS EXIST FOR THE SUN POSITION AT TIME T50 AND
C      KEPLERIAN MOTION USING POS/VEL SUPPLIED. MINOR ERROR.
        IERR = -1
        GO TO 9999
        END IF
C
C CONVERT TRUE ANOMALIES, UMBIN AND UMBOUT, TO MEAN ANOMALIES.
C
      UMBINMA  = ANOMLY(-1,UMBINTA,ELEMS(2),LUERR,IER1)
      UMBOUTMA = ANOMLY(-1,UMBOUTTA,ELEMS(2),LUERR,IER1)
      IF(IER1.NE.0) THEN
        IERR = 1
        GO TO 9999
        END IF
C
C CONVERT MEAN ANOMALY DIFFERENCES TO TIME DIFFERENCES, COMPUTE THE
C RISE/SET TIMES.
C
      TNOWMA = ELEMS(6)
C
      DELANG = EQVANG( UMBOUTMA-TNOWMA )
      TTNRISE = T50 + DELANG/TWOPI * PERIOD
C
      DELANG = EQVANG( UMBINMA-TNOWMA )
      TTNSET  = T50 + DELANG/TWOPI * PERIOD
C
      TTPRISE = TTNRISE - PERIOD
      TTPSET  = TTNSET - PERIOD
C
C DETERMINE CURRENT DAY/NIGHT STATUS.
C
      IF(TTNSET.LT.TTNRISE) THEN
        NOWSTAT = 1  ! SUNSET IS EARLIER, HENCE NOW IN DAYLIGHT.
      ELSE
        NOWSTAT = 2  ! NOW IN ORBIT NIGHT.
        END IF
C
 9999 CONTINUE
      IF(IBUG.NE.0) WRITE(LUBUG,1003) TNOWMA*DEGRAD,UMBINMA*DEGRAD,
     *     UMBOUTMA*DEGRAD,PAKTIM50(TTPRISE),PAKTIM50(TTPSET),
     *     PAKTIM50(TTNRISE),PAKTIM50(TTNSET),IERR,NOWSTAT
 1003 FORMAT(/,' TSHADOW1 DEBUG 1003.'/,
     *  '   TNOWMA=',F6.1,'  UMBINMA=',F6.1,'  UMBOUT=',F6.1/,
     *  '   PREV RISE,SET = ',2F14.6/,
     *  '   NEXT RISE,SET = ',2F14.6/,
     *  '   IERR = ',I2,'   NOWSTAT=',I3)
C
C WRAP-UP.
C
      IF(IERR.EQ.0) THEN            ! NO ERROR
        TNRISE = TTNRISE
        TNSET  = TTNSET
        TPRISE = TTPRISE
        TPSET  = TTPSET
      ELSE IF(IERR.EQ.1) THEN       ! MAJOR ERROR
        TPRISE = 0.D0
        TPSET =  0.D0
        TNRISE = 0.D0
        TNSET =  0.D0
        NOWSTAT = 0
      ELSE IF(IERR.EQ.-1) THEN      ! MINOR ERROR(NO ORBIT NIGHT)
        TPRISE = T50
        TPSET =  T50
        TNRISE = T50
        TNSET =  T50
        NOWSTAT = 1
      ELSE
        STOP ' TSHADOW1 ERROR END. CODING ERROR. BAD IERR VALUE.'
        END IF
C
      RETURN
      END
